﻿package mortal.game.view.lotteryDraw
{
    import Message.DB.Tables.*;
    import Message.Public.*;
    import __AS3__.vec.*;
    import com.greensock.layout.*;
    import com.mui.controls.*;
    import com.mui.utils.*;
    import extend.language.*;
    import fl.controls.*;
    import fl.data.*;
    import flash.events.*;
    import mortal.common.*;
    import mortal.common.global.*;
    import mortal.component.ui.*;
    import mortal.game.cache.*;
    import mortal.game.events.*;
    import mortal.game.mvc.*;
    import mortal.game.resource.*;
    import mortal.game.resource.tableConfig.*;
    import mortal.game.view.common.*;
    import mortal.game.view.common.pageSelect.*;
    import mortal.game.view.shopMall.data.*;
    import mortal.mvc.core.*;

    public class LotteryExchangePanel extends GSprite
    {
        private var _myExchangeRecordBox:lotteryDrawRecordBox;
        private var _bAreaExchangeRecordBox:lotteryDrawRecordBox;
        private var _categorySpr:LotteryExchangeCategorySpr;
        private var _myRecodeBox:lotteryDrawRecordBox;
        private var _bAreaRecodeList:GTileList;
        protected var _scrollPane1:GScrollPane;
        private var _curSelIndex:int = -1;
        private var _exchangeLevel:int = -1;
        private var _shopCode:int = -1;
        private var _coinType:int = -1;
        private var _coinLabel:String = "";
        private var _curGoodsNum:int = 0;
        private const _maxNumPerPage:int = 8;
        private var _goodsList:GTileList;
        private var _diaBox:GConsumeBox;
        private var _consumeBox:GConsumeBox;
        private var _backBtn:GLoadingButton;
        protected var _pageSelecter:PageSelecter;

        public function LotteryExchangePanel()
        {
            return;
        }// end function

        public function set panelData(param1:LotteryExchangeCategoryData) : void
        {
            this._exchangeLevel = param1.level;
            this._shopCode = param1.shopCode;
            var _loc_2:* = ShopConfig.instance.getTShopByCode(this._shopCode);
            this._coinType = _loc_2.unit;
            this._coinLabel = GameDefConfig.instance.getEPrictUnitName(this._coinType);
            if (this._consumeBox)
            {
                this._consumeBox.dispose();
                this._consumeBox = null;
            }
            this._consumeBox = UIFactory.gConsumeBox("", 250 + 115, 580, 0, this);
            if (_loc_2.type == 1)
            {
                this._consumeBox.addItem(ConsumeData.ShowMoneyType, this._coinType, 0, this._coinLabel + ":");
            }
            else
            {
                this._consumeBox.addItem(ConsumeData.ShowItemType, this._coinType, 0, this._coinLabel + ":", "", GameDefConfig.instance.getEPrictUnitImg(this._coinType));
            }
            if (this._pageSelecter)
            {
                this._pageSelecter.currentPage = 1;
            }
            this.updateGoodsList();
            if (this._myRecodeBox)
            {
                this._myRecodeBox.clearData();
            }
            Dispatcher.dispatchEvent(new DataEvent(EventName.LotteryExchangeRequestRecord, {type:3, level:this._exchangeLevel}));
            Dispatcher.dispatchEvent(new DataEvent(EventName.LotteryExchangeRequestRecord, {type:4, level:this._exchangeLevel}));
            return;
        }// end function

        private function switchTag(param1:int) : void
        {
            if (this._categorySpr && !this._categorySpr.isDisposed)
            {
                this._categorySpr.switchTag(param1);
            }
            return;
        }// end function

        private function onSwitchHandle(event:DataEvent) : void
        {
            var _loc_3:LotteryExchangeCategoryData = null;
            var _loc_2:* = int(event.data);
            if (this._curSelIndex != _loc_2)
            {
                this._curSelIndex = _loc_2;
                _loc_3 = this._categorySpr.selectItemData;
                if (_loc_3)
                {
                    this.panelData = _loc_3;
                }
            }
            return;
        }// end function

        override protected function createDisposedChildrenImpl() : void
        {
            super.createDisposedChildrenImpl();
            this.pushUIToDisposeVec(UIFactory.bg(8, 63, 648, 516, this));
            this._categorySpr = UICompomentPool.getUICompoment(LotteryExchangeCategorySpr);
            this._categorySpr.x = 8;
            this._categorySpr.y = 62;
            addChild(this._categorySpr);
            this._goodsList = UIFactory.tileList(8, 105, 700, 480, this);
            this._goodsList.columnWidth = 317;
            this._goodsList.rowHeight = 109;
            this._goodsList.horizontalGap = 8;
            this._goodsList.verticalGap = 0;
            this._goodsList.setStyle("cellRenderer", lotteryExchangeGoodsCellRender);
            this._goodsList.isClickedUpdate = false;
            this._goodsList.isChangeUpdate = false;
            this._goodsList.isClickedUpdate = false;
            var _loc_1:* = GlobalStyle.textFormatBai;
            _loc_1.align = AlignMode.CENTER;
            this._pageSelecter = UIFactory.pageSelecter(267, 450 + 101, this, PageSelecter.CompleteMode);
            this._pageSelecter.setbgStlye(ImagesConst.ComboBg, _loc_1);
            this._pageSelecter.pageTextBoxSize = 50;
            this._pageSelecter.configEventListener(Event.CHANGE, this.onPageChange);
            this._pageSelecter.currentPage = 1;
            this._diaBox = UIFactory.gConsumeBox("", 50 + 115, 580, 0, this);
            this._diaBox.addItem(ConsumeData.ShowMoneyType, EPriceUnit._EPriceUnitGold, 0, Language.getString(90448));
            this._backBtn = UIFactory.gLoadingButton(ResFileConst.lotteryDrawReturnBtn, 500, 600, -1, -1, this);
            this._backBtn.visible = false;
            this.pushUIToDisposeVec(UIFactory.bg(14 + 640 + 4, 24 + 40 - 4, 270, 546, this));
            this.pushUIToDisposeVec(UIFactory.gBitmap(ImagesConst.SkillTitle_nameBg, 14 + 640 + 4, 24 + 40 - 4, this));
            this.pushUIToDisposeVec(UIFactory.gBitmap(ImagesConst.SkillTitle_nameBg, 14 + 640 + 4, 300 + 40 - 4, this));
            this.pushUIToDisposeVec(UIFactory.gImageBitmap(ImagesConst.lotteryExchangeMyRecordTxt, 14 + 640 + 4, 29 + 40 - 4, this));
            this.pushUIToDisposeVec(UIFactory.gImageBitmap(ImagesConst.lotteryExchangeBAreaRecordTxt, 14 + 640 + 4, 305 + 40 - 4, this));
            this._myRecodeBox = new lotteryDrawRecordBox(10);
            this.addChild(this._myRecodeBox);
            this._bAreaRecodeList = UIFactory.tileList(20 + 640 + 4, 330 + 34, 240, 240, this);
            this._bAreaRecodeList.columnWidth = 240;
            this._bAreaRecodeList.rowHeight = 50;
            this._bAreaRecodeList.horizontalGap = 5;
            this._bAreaRecodeList.setStyle("cellRenderer", lotteryDrawRecodeCellRender);
            this._scrollPane1 = UIFactory.gScrollPanel(20 + 640 + 4, 55 + 32, 260, 240, this);
            this._scrollPane1.verticalScrollPolicy = ScrollPolicy.AUTO;
            this._scrollPane1.horizontalScrollPolicy = ScrollPolicy.OFF;
            this._scrollPane1.source = this._myRecodeBox;
            this.addChild(this._scrollPane1);
            this._scrollPane1.update();
            Dispatcher.addEventListener(EventName.LotteryExchangeSwitchPanel, this.onSwitchHandle);
            var _loc_2:* = Cache.instance.role.entityInfo.level;
            var _loc_3:* = this._categorySpr.dataProvider;
            this.switchTag((_loc_3.length - 1));
            return;
        }// end function

        private function getMyRecodeListDataProvider() : DataProvider
        {
            var _loc_1:* = new DataProvider();
            var _loc_2:* = Cache.instance.lotteryExchangeRecord.myRecordInfo;
            var _loc_3:int = 0;
            while (_loc_3 < _loc_2.length)
            {
                
                _loc_1.addItem({recordInfo:_loc_2[_loc_3], recordType:lotteryRecordType.Record_Exchange_Mine});
                _loc_3++;
            }
            return _loc_1;
        }// end function

        private function getbAreaRecodeListDataProvider() : DataProvider
        {
            var _loc_1:* = new DataProvider();
            var _loc_2:* = Cache.instance.lotteryExchangeRecord.bAreaRecordInfo;
            var _loc_3:int = 0;
            while (_loc_3 < _loc_2.length)
            {
                
                _loc_1.addItem({recordInfo:_loc_2[_loc_3], recordType:lotteryRecordType.Record_Exchange_BArea});
                _loc_3++;
            }
            return _loc_1;
        }// end function

        public function updateMyRecord(param1:int) : void
        {
            if (this.isDisposed)
            {
                return;
            }
            if (param1 != this._exchangeLevel)
            {
                return;
            }
            this._myRecodeBox.dataProvider = this.getMyRecodeListDataProvider();
            this._myRecodeBox.validateNow();
            this._myRecodeBox.drawNow();
            this._scrollPane1.validateNow();
            this._scrollPane1.update();
            this._scrollPane1.verticalScrollPosition = 0;
            return;
        }// end function

        public function updateBAreaRecord(param1:int) : void
        {
            if (this.isDisposed)
            {
                return;
            }
            if (param1 != this._exchangeLevel)
            {
                return;
            }
            this._bAreaRecodeList.dataProvider = this.getbAreaRecodeListDataProvider();
            return;
        }// end function

        private function updateGoodsList() : void
        {
            var _loc_1:Vector.<TShopSell> = null;
            var _loc_2:DataProvider = null;
            if (this._goodsList)
            {
                _loc_1 = ShopConfig.instance.getShopSellItemByShopCode(this._shopCode);
                _loc_2 = this.getBuyDataProvider(this._shopCode, this._pageSelecter.currentPage);
                this._goodsList.dataProvider = _loc_2;
                this._curGoodsNum = _loc_1.length;
                this._goodsList.drawNow();
                this.updatePageSelecter();
            }
            return;
        }// end function

        protected function getBuyDataProvider(param1:int, param2:int) : DataProvider
        {
            var _loc_6:int = 0;
            var _loc_7:int = 0;
            var _loc_8:int = 0;
            var _loc_9:ShopItemData = null;
            var _loc_10:Object = null;
            var _loc_3:* = new DataProvider();
            var _loc_4:* = ShopConfig.instance.getShopSellItemByShopCode(int(param1));
            var _loc_5:* = this._maxNumPerPage;
            if (_loc_4)
            {
                _loc_6 = _loc_5 * (param2 - 1);
                if (_loc_5 * param2 <= _loc_4.length)
                {
                    _loc_7 = _loc_5 * param2;
                }
                else
                {
                    _loc_7 = _loc_4.length;
                }
                _loc_8 = _loc_6;
                while (_loc_8 < _loc_7)
                {
                    
                    _loc_9 = new ShopItemData(_loc_4[_loc_8]);
                    _loc_10 = {data:_loc_9, type:1};
                    _loc_3.addItem(_loc_10);
                    _loc_8++;
                }
            }
            return _loc_3;
        }// end function

        protected function updatePageSelecter() : void
        {
            this._pageSelecter.maxPage = Math.ceil(this._curGoodsNum / this._maxNumPerPage);
            return;
        }// end function

        private function onPageChange(event:Event) : void
        {
            this.updateGoodsList();
            return;
        }// end function

        override protected function disposeImpl(param1:Boolean = true) : void
        {
            Dispatcher.removeEventListener(EventName.LotteryExchangeSwitchPanel, this.onSwitchHandle);
            this._categorySpr.dispose(param1);
            this._categorySpr = null;
            this._goodsList.dispose(param1);
            this._goodsList = null;
            this._pageSelecter.dispose(param1);
            this._pageSelecter = null;
            this._diaBox.dispose(param1);
            this._diaBox = null;
            this._backBtn.dispose(param1);
            this._backBtn = null;
            if (this._consumeBox)
            {
                this._consumeBox.dispose(param1);
                this._consumeBox = null;
            }
            this._myRecodeBox.dispose(param1);
            this._scrollPane1.dispose(param1);
            this._myRecodeBox = null;
            this._scrollPane1 = null;
            this._bAreaRecodeList.dispose(param1);
            this._bAreaRecodeList = null;
            this._curSelIndex = -1;
            super.disposeImpl(param1);
            return;
        }// end function

    }
}
